package org.lw.ssm.controller;


import org.lw.ssm.Entity.Department;
import org.lw.ssm.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/departments")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;  // 注入服务层

    // 获取所有部门
    @GetMapping
    public List<Department> getAllDepartments() {
        return departmentService.getAllDepartments();
    }

    // 根据ID获取某个部门信息
    @GetMapping("/{id}")
    public Department getDepartmentById(@PathVariable Long id) {
        return departmentService.getDepartmentById(id);
    }

    // 创建新的部门
    @PostMapping
    public void createDepartment(@RequestBody Department department) {
        departmentService.createDepartment(department);
    }

    // 更新部门信息
    @PutMapping("/{id}")
    public void updateDepartment(@PathVariable Long id, @RequestBody Department department) {
        department.setId(id); // 设置要更新的部门ID
        departmentService.updateDepartment(department);
    }

    // 删除部门
    @DeleteMapping("/{id}")
    public void deleteDepartment(@PathVariable Long id) {
        departmentService.deleteDepartment(id);
    }
}
